package com.maixy.esdemo.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;

/**
 * @ClassName ESTestDocInsert
 * @Description TODO
 * @Date 2021/4/16 22:40
 * @Author maixy
 */
public class ESTestDocInsertQuery {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient esClient =new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200, "http")));

        //SearchRequest request = matchAllQuery();
        //SearchRequest request = termQuery();
        //SearchRequest request = matchQueryByPage();
        //SearchRequest request = combinationQuery();
        //SearchRequest request = rangQuery();
        //SearchRequest request = fuzzyQuery();
        SearchRequest request = aggregationQuery();
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        System.out.println(response);
        SearchHits searchHits = response.getHits();
        System.out.println(searchHits.getTotalHits());
        System.out.println(response.getTook());

        for (SearchHit hit : searchHits) {
            System.out.println(hit.getSourceAsString());
        }
        esClient.close();
    }

    /**
     * 查询索引中全部数据
     * @param esClient --
     * @throws IOException --
     */
    public static SearchRequest matchAllQuery(RestHighLevelClient esClient) throws IOException {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
        return request;
    }

    /**
     * 条件查询
     *
     */
    public static SearchRequest termQuery() {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("name", "王")));

        return request;
    }

    /**
     * 分页查询
     */
    public static SearchRequest matchQueryByPage() {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.from(0);
        builder.size(4);
        //排序
        builder.sort("age", SortOrder.DESC);
        //排除或包含
        String[] includes = {"name","age"};
        String[] excludes = {};
        builder.fetchSource(includes, excludes);
        request.source(builder);
        return request;
    }

    /**
     * 组合条件查询
     */
    public static SearchRequest combinationQuery() {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //and
        //boolQueryBuilder.must(QueryBuilders.matchQuery("age", 59));
        //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男"));

        //or
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 59));
        boolQueryBuilder.should(QueryBuilders.matchQuery("age", 19));

        builder.query(boolQueryBuilder);
        request.source(builder);
        return request;
    }

    /**
     * 范围查询
     */
    public static SearchRequest rangQuery() {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age");
        rangeQueryBuilder.gte(20);
        rangeQueryBuilder.lte(60);

        builder.query(rangeQueryBuilder);
        request.source(builder);
        return request;
    }

    /**
     * 模糊查询,高亮查询
     */
    public static SearchRequest fuzzyQuery() {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
        highlightBuilder.field("name");
        builder.highlighter(highlightBuilder);
        builder.query(QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.ONE));
        request.source(builder);
        return request;
    }

    /**
     * 聚合查询
     */
    public static SearchRequest aggregationQuery() {
        SearchRequest request = new SearchRequest();
        request.indices("user");
        SearchSourceBuilder builder = new SearchSourceBuilder();

        //AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
        AggregationBuilder aggregationBuilder = AggregationBuilders.terms("groupAge").field("age");
        builder.aggregation(aggregationBuilder);


        request.source(builder);
        return request;
    }
}
